﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Basic.第一章
{
    public delegate void StringAssignmentEventHandler();//抽象的操作方法。

    public class InvokeList
    {
        private readonly IList<StringAssignmentEventHandler> _handlers;
        private readonly string[] _message = new string[3];
        private readonly string[] _message_single = new string[3];
        private readonly StringAssignmentEventHandler _handler;
        public InvokeList()
        {
            //绑定一组抽象方法
            //方式1
            _handler += delegate { _message_single[0] = "world"; };
            _handler += delegate { _message_single[1] = ","; };
            _handler += delegate { _message_single[2] = "hello"; };//匿名方法调用


            //方式2
            _handlers = new List<StringAssignmentEventHandler> { AppendHello, AppendComma, AppendWorld };
        }

        public void Invoke()
        {
            _handler?.Invoke();
            foreach (var handler in _handlers)
            {
                handler();
            }
        }

        public string this[int index] => _message[index];
        public string[] Message => _message_single;
        public void AppendHello() { _message[0] = "Hello"; }
        public void AppendComma() { _message[1] = ","; }
        public void AppendWorld() { _message[2] = "world"; }
    }
}
